//
//  MainTabBar.swift
//  AAWeibo
//
//  Created by 上官风雨 on 15/11/11.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   func setupUI(){
    
      addSubview(composeBtn)
    
    }
    
    //撰写按钮的懒加载
    
    lazy var composeBtn : UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        btn.addTarget(self, action: "composeDidClick", forControlEvents: .TouchUpInside)
               
        return btn
    }()
    
    //重新布局子控件
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = self.bounds.width / 5
        let h = self.bounds.height
        
        let cgrect = CGRectMake(0, 0, w, h)
        
        var index :CGFloat = 0
        
        for subview in subviews{
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                subview.frame = CGRectOffset(cgrect, index*w, 0)
                
                index += index == 1 ? 2 : 1
            }
            
            
            
        }
        //给添加按钮设置frame
        composeBtn.frame = CGRectMake(2 * w, 0, w, h)
        
    }
    
    func composeDidClick(){
        
        print("composeBtn被点击啦")
    }

}
